﻿/*
	清除錯誤訊息區
*/
function ErrHandler_ClearItem()
{	
	//if(parent.document.all[sFieldId] == undefined) return;
	
	for(var i=ErrMsg.ErrMsgTable.rows.length-1; i>0; i--)
	{
		var sFieldId = ErrMsg.ErrMsgTable.rows[i].cells[2].innerText;
		//parent.document.all[sFieldId].style.background = "#FFFFFF";
		document.getElementById(sFieldId).style.background = "#FFFFFF";
		ErrMsg.ErrMsgTable.deleteRow(i);
	}
	if(ErrMsg.ErrMsgTable.rows.length>0)
	{
		ErrMsg.ErrMsgTable.deleteRow(0);
	}
}
/*
	加入錯誤訊息區
*/
function ErrHandler_AddItem(o,Isalert)
{	
	var IsExistErr = false;
	//秀錯誤訊息
	//MARK BY PONY
	/*if(!self.ErrMsg.document.all["IsOpenMsg"].checked && Isalert) {
		alert(o.ErrMsg);
	}
	*/
	if(Isalert) {
		alert(o.ErrMsg);
	}
	if(o.ErrCode==undefined){
		o.ErrCode="";
	}
	//焦距欄位 
	// document.all["btnAdd"].style.backgroundImage = "url(img/disSave.gif)";
	
	//填入錯誤訊息區的欄位名稱

	if(ErrMsg.ErrMsgTable.rows.length==0)
	{
		var oTR = ErrMsg.ErrMsgTable.insertRow(0);
		oTR.id = "title"; 
		//NewCell
		var oTD = oTR.insertCell(0);
		with (oTD)
		{
			style.width = "30%";
			className = "errorheader";
			innerHTML = "<NOBR>欄位名稱</NOBR>";
		}
		//NewCell
		var oTD = oTR.insertCell(1);
		with (oTD)
		{
			style.width = "70%";
			className = "errorheader";
			innerHTML = "<NOBR>訊息</NOBR>";
		}
		//NewCell
		var oTD = oTR.insertCell(2);
		with (oTD)
		{
			style.display = "none";
			className = "errorheader";
			innerHTML = "<NOBR>欄位編號</NOBR>";
		}
	}//end if
		
		
	//更新相同錯誤欄位(欄位名稱,錯誤訊息,欄位編號)
	for(var i=1;i<ErrMsg.ErrMsgTable.rows.length;i++)
	{
		var sFieldId = ErrMsg.ErrMsgTable.rows[i].cells[2].innerText;
		var sFieldErrCode = ErrMsg.ErrMsgTable.rows[i].cells[1].children[1].value;
		if(o.id == sFieldId )
		{
			IsExistErr = true;
			//判斷如果是OnTextChange產生則UPDATE錯誤訊息
			if(o.ErrCode=="" && sFieldErrCode==""){
			ErrMsg.ErrMsgTable.rows[i].cells[1].children[0].innerText = o.ErrMsg;
			ErrMsg.ErrMsgTable.rows[i].cells[1].children[1].value = o.ErrCode;
			break;
			}
			else{
			//判斷如果是OnTextChange產生則UPDATE錯誤訊息
			break;
			}
			
		}
	
	}
		
	//填入第一個錯誤欄位(欄位名稱,錯誤訊息,欄位編號)
	if(!IsExistErr)
	{
		var oTR = ErrMsg.ErrMsgTable.insertRow(1);
		//NewCell FieldName
		var oTD = oTR.insertCell(0);
		with (oTD)
		{
			style.width = "30%";
			className = "detail";
			innerHTML = "<NOBR>" + o.Name + "</NOBR>";
		}
		//NewCell ErrorMessage
		var oTD = oTR.insertCell(1);
		with (oTD)
		{
			style.width = "70%";
			className = "detail";
			innerHTML = "<NOBR>" + ToUpperString(o.ErrMsg) + "</NOBR>"+"<INPUT type='hidden' value='"+o.ErrCode+"'>";
		}
		//NewCell FieldID
		var oTD = oTR.insertCell(2);
		with (oTD)
		{
			style.display = "none";
			className = "detail";
			innerHTML = "<NOBR>" + o.id + "</NOBR>";
		}
		
	}//end if			
}//end function


/*
	隱藏錯誤訊息 
	add by samantha 2005/5/11
*/
function ErrHandler_Visible_Item(o,Visible)
{
	for(var i=1;i<ErrMsg.ErrMsgTable.rows.length;i++)
	{
		var sFieldId = ErrMsg.ErrMsgTable.rows[i].cells[2].innerText;
		if(o.id == sFieldId)
		{
			//如果傳true不隱藏

			if(Visible){
				document.getElementById(sFieldId).style.background = "#FFDFDC"; 
				ErrMsg.ErrMsgTable.rows[i].style.display="block";
			}
			//如果傳false隱藏
			else{
			    document.getElementById(sFieldId).style.background = "#FFFFFF";
				ErrMsg.ErrMsgTable.rows[i].style.display="none";
			}
		}//end if
		
	}//end for
}

/*
	移除錯誤訊息區
*/
function ErrHandler_RemoveItem(o)
{
	//更新相同錯誤欄位(欄位名稱,錯誤訊息,欄位編號)
	if(o.ErrCode==undefined){
		o.ErrCode="";
	}
	for(var i=1;i<ErrMsg.ErrMsgTable.rows.length;i++)
	{
		var sFieldId = ErrMsg.ErrMsgTable.rows[i].cells[2].innerText;
	
		var sFieldErrCode = ErrMsg.ErrMsgTable.rows[i].cells[1].children[1].value;
	    
		if(o.id == sFieldId)
		{
			//判斷ErrCode沒有東西則清除自己本身產生所有的錯誤訊息
			if(o.ErrCode==""){
				ErrMsg.ErrMsgTable.deleteRow(i);
				//top.document.all[sFieldId].style.background = "#FFFFFF";
				//parent.document.all[sFieldId].style.background = "#FFFFFF";
				//20050407 MODIFY BY PONY
				document.getElementById(sFieldId).style.background = "#FFFFFF";
				if(ErrMsg.ErrMsgTable.rows.length==1)
				{
					ErrMsg.ErrMsgTable.deleteRow(0);
					if (document.getElementById("btnAdd") != undefined)
					{
					    document.getElementById("btnAdd").style.backgroundImage = "url(img/Save.gif)";
					}
				}
				break;
			}
			else{
				//判斷ErrCode有東西則清除自己本身產生和ErrCode相同錯誤訊息
				if(o.ErrCode==sFieldErrCode){
					ErrMsg.ErrMsgTable.deleteRow(i);
					//top.document.all[sFieldId].style.background = "#FFFFFF";
					//parent.document.all[sFieldId].style.background = "#FFFFFF";
					//20050407 MODIFY BY PONY
					document.getElementById(sFieldId).style.background = "#FFFFFF";
					if(ErrMsg.ErrMsgTable.rows.length==1)
					{
						ErrMsg.ErrMsgTable.deleteRow(0);
						if(document.getElementById("btnAdd")!=undefined) 
						{
						    document.getElementById("btnAdd").style.backgroundImage = "url(img/Save.gif)";
						}
					}
					break;
				}
			
			}

		}//end if
		
	}//end for
}

/*
	檢查畫面上所有元件的資料
*/
function isValid(o) 
{
    // 檢核欄位格式
    // 宣告回傳之boolean變數
	var bolValid = true;
	
	try
	{
	    //ErrHandler_ClearItem();
	    for(i = o.length - 1; i >= 0; i--)
	    {	
			if((o[i].type=="text" || o[i].type == "textarea") && !o[i].readOnly  ){
				//只清除isValid產生的錯誤訊息add by samantha
				var objControl = new Object();
				objControl.ErrCode="999"
 				objControl.id =o[i].id;
 				ErrHandler_RemoveItem(objControl)
				//檢查必要輸入欄位
				if (o[i].AllowEmpty == "False" && o[i].value.trim() == "")
				{
					//addErrorItem(o[i],"不可空白，請輸入");
					AddErrorFromIsValid(o[i],"不可空白，請輸入","999");
					bolValid = false;
				}
				//檢查文字欄位add by samantha 20050609
				if(o[i].TypeMode == "text" && o[i].value.trim() != "")
				{   
					// 檢核是否符合dataFormat
					if (o[i].DataFormat != undefined && o[i].DataFormat.trim() != "")
					{   
						format = new RegExp(o[i].DataFormat);
						if (!format.test(o[i].value.trim()))
						{
							//addErrorItem(o[i],"數字格式不正確!");
							AddErrorFromIsValid(o[i],"文字格式不正確!","999");
							bolValid = false;
						}
					}
				}
	    		
				//檢查數字欄位
				if(o[i].TypeMode == "number" && o[i].value.trim() != "")
				{   
					// 檢核是否為數值

					if(isNaN(o[i].value))
					{
						//addErrorItem(o[i],"請輸入數字");
						AddErrorFromIsValid(o[i],"請輸入數字","999");
						bolValid = false;
					}
					// 檢核是否符合dataFormat
					else if (o[i].DataFormat != undefined && o[i].DataFormat.trim() != "")
					{   
						format = new RegExp(o[i].DataFormat);
						if (!format.test(o[i].value.trim()))
						{
							//addErrorItem(o[i],"數字格式不正確!");
							AddErrorFromIsValid(o[i],"數字格式不正確!","999");
							bolValid = false;
						}
					}
				}
	    		
				//檢查日期欄位	
				if(o[i].TypeMode == "date" && o[i].value.trim()  !="")
				{   
					if (o[i].DataFormat != undefined && o[i].DataFormat.trim() != "")
					{
						format = new RegExp(o[i].DataFormat);
					}
					else
					{
						//檢查閏年判斷式

						//if(eval(o[i].value.substr(0,4))%4 == 0){		//modify by cedric
						if((!isNaN(o[i].value.substr(0,4))) && (o[i].value.trim().substr(0,4)%4 == 0)){
							format = new RegExp(/(^\d{4}02[1-2][0-9]$)|(^\d{4}02[0][1-9]$)|(^\d{4}((0[13578])|(1[02]))(([0][1-9])|([1-2]\d{1})|(3[0-1]))$)|(^\d{4}((0[469])|(11))(([0][1-9])|([1-2]\d{1})|(30))$)/);
						}
						else{
							format = new RegExp(/(^\d{4}02[1][0-9]$)|(^\d{4}02[2][0-8]$)|(^\d{4}02[0][1-9]$)|(^\d{4}((0[13578])|(1[02]))(([0][1-9])|([1-2]\d{1})|(3[0-1]))$)|(^\d{4}((0[469])|(11))(([0][1-9])|([1-2]\d{1})|(30))$)/);
						}
						
					}
	    		    
					if (!format.test(o[i].value.trim()))
					{
						//addErrorItem(o[i],"日期格式不正確!");
						AddErrorFromIsValid(o[i],"日期格式不正確!","999");
						bolValid = false;
					}
				}
	    		
				//檢查時間欄位	
				if(o[i].TypeMode == "time" &&  o[i].value.trim()  !="")
				{
					if (o[i].DataFormat != undefined && o[i].DataFormat.trim() != "")
					{
						format = new RegExp(o[i].DataFormat);
					}
					else
					{
						format = new RegExp(/(^(([0-1]\d)|(2[0-3]))[0-5]\d[0-5]\d$)|(^24[0]{4}$)/);
					}
					if (!format.test(o[i].value.trim()))
					{
						//addErrorItem(o[i],"時間格式不正確");
						AddErrorFromIsValid(o[i],"時間格式不正確","999");
						bolValid = false;
					}
				}
		    }
		    
	    }
	     //判斷錯誤訊息區是否有錯誤add by samantha
	    for(var i=1;i<ErrMsg.ErrMsgTable.rows.length;i++)
		{
			if(ErrMsg.ErrMsgTable.rows[i].style.display!="none"){
				bolValid = false;
			}
		}//end for
		
	}
	catch (e) 
	{
	    throw "Error : isValid()" + e.message;
	}
	finally
	{
	}
	if(!bolValid){
		ShowError("請針對訊息區發生的錯誤進行修正,才允許"+event.srcElement.value+"!!")
	}
	return bolValid;
}
//for isValid使用，主要在判斷是Detail發生錯誤或Master發生錯誤，依錯誤訊息呼叫適合的function
//add by cedric --20050323
function AddErrorFromIsValid(obj,msg,errCode)
{
	var tableID = obj.parentElement.parentElement.parentElement.parentElement.id;
	if (tableID.toUpperCase().substr(0, 11) == "DETAILTABLE")
	{
		var obj1 = new Object();
		obj1.ErrCode = errCode;
		obj1.id = obj.name;
		var titleName = "";
		try
		{
			titleName = document.getElementById(obj.id.replaceAll("txt","lbl")).innerHTML;
		}catch(e){
		
		}
		addDetailErrorItem(obj1,msg,titleName);
	}
	else
	{
		addErrorItem(obj,msg,errCode);
	}


}


/*
	檢查單一元件的資料

	若發生錯誤回傳一個錯誤訊息，若沒有錯誤則回傳空字串("")
*/
function isValidElement(o) 
{
    // 檢核欄位格式
    // 宣告回傳之訊息變數

	var msgValid = "";
	
	
	try
	{
	    var objControl = new Object();
		objControl.id = o.id;
		try
		{
			objControl.Name = document.getElementById(o.id.replaceAll("txt","lbl")).innerHTML;
		}
		catch(ex)
		{
			objControl.Name = "";
		}
		    //檢查必要輸入欄位
		    if (o.AllowEmpty == "False" && o.value == "")
		    {
                msgValid = objControl.Name+"不可空白，請輸入";
		    }
		    //檢查數字欄位
		    if(o.TypeMode == "number" && o.value != "")
		    {   
		        // 檢核是否為數值

		        if(isNaN(o.value))
		        {
                    
			        msgValid = objControl.Name+"請輸入數字";
			    }
			    // 檢核是否符合dataFormat
			    else if (o.DataFormat != undefined && o.DataFormat.trim() != "")
			    {   
			        format = new RegExp(o.DataFormat);
			        if (!format.test(o.value))
			        {
                        
			            msgValid = objControl.Name+"數字格式不正確!";
			        }
			    }
		    }
    		
		    //檢查日期欄位	
		    if(o.TypeMode == "date" && o.value !="")
		    {   
		        if (o.DataFormat != undefined && o.DataFormat.trim() != "")
		        {
		            format = new RegExp(o.DataFormat);
		        }
		        else
		        {
					//檢查閏年判斷式

					//if(eval(o.value.substr(0,4))%4 == 0){	//modify by cedric
						if((!isNaN(o.value.substr(0,4))) && (o.value.substr(0,4)%4 == 0)){
							format = new RegExp(/(^\d{4}02[1-2][0-9]$)|(^\d{4}02[0][1-9]$)|(^\d{4}((0[13578])|(1[02]))(([0][1-9])|([1-2]\d{1})|(3[0-1]))$)|(^\d{4}((0[469])|(11))(([0][1-9])|([1-2]\d{1})|(30))$)/);
						}
						else{
							format = new RegExp(/(^\d{4}02[1][0-9]$)|(^\d{4}02[2][0-8]$)|(^\d{4}02[0][1-9]$)|(^\d{4}((0[13578])|(1[02]))(([0][1-9])|([1-2]\d{1})|(3[0-1]))$)|(^\d{4}((0[469])|(11))(([0][1-9])|([1-2]\d{1})|(30))$)/);
						}
					
					
		        }
    		    
		        if (!format.test(o.value))
		        {
                   msgValid = objControl.Name+"日期格式不正確!";
			    }
		    }
    		
		    //檢查時間欄位	
		    if(o.TypeMode == "time" &&  o.value !="")
		    {
		        if (o.DataFormat != undefined && o.DataFormat.trim() != "")
		        {
		            format = new RegExp(o.DataFormat);
		        }
		        else
		        {
		            format = new RegExp(/(^(([0-1]\d)|(2[0-3]))[0-5]\d[0-5]\d$)|(^24[0]{4}$)/);
		        }
		        if (!format.test(o.value))
		        {
                    
			        msgValid = objControl.Name+"時間格式不正確";
			    }
		    }
	    
	}
	catch (e) 
	{
	    throw "Error : isValid()" + e.message;
	}
	finally
	{
	}
	return msgValid;
}
/*  將Server檢查結果XML錯誤訊息Bind訊息區 */
function ErrorBind(XMLElement)
{
	try
	{
		var NodeList = XMLElement.selectNodes("//Item");
		var objErrItem = new Object();
		
		var IsError = false;
		for(var i=0 ; i<NodeList.length ; i++){
			IsError = true;
			objErrItem.id = NodeList(i).getAttribute("id");
			addDetailErrorItem(objErrItem,NodeList(i).getAttribute("message"),NodeList(i).getAttribute("name"));
		}
		return IsError;
	}
	catch(e)
	{
		ShowError(e.message);
	}
}

function addErrorItem(o,message,errCode) 
{   
    var objControl = new Object();
    objControl.id = o.id;
    try
    {
		//change by vicky
		if(o.id.substr(0,3) == 'drp')
		{
				objControl.Name = document.getElementById(o.id.replaceAll("drp","lbl")).innerHTML;
		}
		else
		{
			objControl.Name = document.getElementById(o.id.replaceAll("txt","lbl")).innerHTML;
		}
	}
	catch(e)
	{
		objControl.Name ="";
	}
	if(errCode!=undefined){
	objControl.ErrCode=errCode;
	}
	objControl.ErrMsg = objControl.Name + message;
	ErrHandler_AddItem(objControl,false);
}
function addDetailErrorItem(o,message,name) 
{   
    var objControl = new Object();
    objControl.id = o.id;
    if(o.ErrCode!=undefined){
		objControl.ErrCode = o.ErrCode;
    }
    try
    {
		objControl.Name = document.getElementById(o.id.replaceAll("txt","lbl")).innerHTML;
	}
	catch(e)
	{
		objControl.Name ="";
	}
	objControl.Name = name;
	objControl.ErrMsg = objControl.Name + message;
	ErrHandler_AddItem(objControl,false);
}

// 清除所有textBox之內容 
function clearText(o) 
{
    ErrHandler_ClearItem();
    for(i=0;i<o.length;i++)
    {
        if(o[i].type.trim() == "text" && ! o[i].readOnly && ! o[i].disabled)
        {
            o[i].value = "";
        }
    }
}
function getErrMsg(id) 
{
    switch(id)
		{
			case "1":
					msg="不正確！";break;
			case "2":
					msg="不可空白！"
					break;
			case "3":
					msg="之數量"
					break;
			case "4":
					msg="不存在！"
					break;
			case "5":
					msg="請輸入"
					break;
			case "6":
					msg="不可小於最低訂購數量"
					break;
			case "7":
					msg="的"
					break;
			case "8":
					msg="包裝數量無法成箱"
					break;
			case "9":
					msg="不可輸入"
					break;
			case "10":
					msg="必需輸入數字"
					break;
			case "11":
					msg="尚未開始採購"
					break;
			case "12":
					msg="已停止採購"
					break;
			case "13":
					msg="不可小於系統日"
					break;
			case "14":
					msg="日期格式不正確"
					break;
			case "15":
					msg="資料設定未完整"
					break;
			case "16":
					msg="不可為零"
					break;
			case "17":
					msg="必需輸入八碼"
					break;
			case "18":
					msg="不屬於您負責的範圍！"
					break;
			case "19":
					msg="出貨點尚未設定！"
					break;
			case "20":
					msg="格式錯誤！"
					break;
			case "21":
					msg="已失效！"
					break;
			case "22":
					msg="不可輸入VIP標準裝載！"
					break;
			case "23":
					msg="只可輸入VIP標準裝載！"
					break;
			case "24":
					msg="不可為台幣"
					break;
			case "25":
					msg="不可為美金"
					break;
			case "26":
					msg="為保稅倉料號,故料庫不可為一般品庫房(MANG)"
					break;
			case "27":
					msg="不可為一般品庫房(MANG)"
					break;
			case "28":
					msg="已撤站!"
					break;
			case "29":
					msg="不屬於此廠商所提供！"
					break;
			case "30":
					msg="最低訂購數量為"
					break;
			case "31":
					msg="兌換比為"
					break;	
			case "32":
					msg="主管尚未核決故不可請購！"
					break;		
			case "33":
					msg="為保稅倉料號"
					break;
			case "34":
					msg="請選擇！"
					break;
			case "35":
					msg="當請購類別為餐管類時必需輸入餐管類之料號！"
					break;
			case "36":
					msg="當請購類別為非餐管類時必需輸入非餐管類之料號！"
					break;
		}
		return msg;
    
}

//-------------------------------------------------------------------------------------------------------------------------------
function IsExistError()
{
		var bolValid = true;
	   //判斷錯誤訊息區是否有錯誤add by samantha
	    for(var i=1;i<ErrMsg.ErrMsgTable.rows.length;i++)
		{
			if(ErrMsg.ErrMsgTable.rows[i].style.display!="none"){
				bolValid = false;
			}
		}//end for
		return !bolValid;
}